#! /bin/bash
# thi script is to gen the callback moudle of gen_server
# Uage: ./gen_server ModuleName
Temp="gen_server_template"

if ! [ -f ${Temp} ] || ! [ -r ${Temp} ]; then
    echo "the ${Temp} file not exit or can't readable"
    exit 1
fi

if [ -z "$1" ]; then
    echo "pleae specify the gen_server module name!"
    exit 1
fi

# check the module name
Mod=$(echo $1 | grep -E '[a-z]+[a-zA-Z0-9_@]+')

if [ -z ${Mod} ]; then
    echo "the module name is invalid, please change"
    exit 1
fi

# the Out path
OutPath=${2:-./}
Offset=$((${#OutPath} - 1))
Last=${OutPath:Offset}
#echo "$Last"
if  [ Last != "/" ]; then
    OutPath=${OutPath}/
fi

OutFile=${OutPath}${Mod}.erl

#gen the module
sed "s/-module()./-module(${Mod})./" ${Temp} > ${OutFile}

echo -e "gen uccessly!\n output file:${OutFile}"
